package worldinruin.gui.imp.gameview;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.Action;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.LayoutStyle;

import worldinruin.controllers.ControlFactory;
import worldinruin.controllers.ControlFactory.Controllers;
import worldinruin.core.ISoldier;
import worldinruin.core.ITroop;
import worldinruin.resources.Components;
import worldinruin.resources.Keys;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class DivideTroopsPanel extends javax.swing.JPanel {
	
	
	private Action listener;
	private JPanel divideUnitComponentsPanel;
	private JButton divideTroopButton;
	private ArrayList<DivideUnitComponent> divideUnitComponents = new ArrayList<DivideUnitComponent>();
	private ITroop troop;
	
	/**
	 * Creates a divide troops panel of the given troops.
	 * @param troop Troops to divide.
	 */
	public DivideTroopsPanel(ITroop troop) {
		super();
		this.troop = troop;
		initGUI();
		setupGUI(troop);
		initListener();
		
	}
	
	/**
	 * Initiates listeners to components in the GUI.
	 */
	private void initListener() {
		listener = ControlFactory.getControl(Controllers.GAMEBOARD_CONTROLLER);
		divideTroopButton.addActionListener(listener);
		listener.putValue(Components.divideTroopButton, divideTroopButton);
		listener.putValue(Keys.DIVIDETROOP.toString(), this);
	}
	
	/**
	 * Gets the first divided troops amount and units.
	 * @return Troops unit and amount.
	 */
	public HashMap<ISoldier, Integer> getTroop1Values() {
		HashMap<ISoldier, Integer> values = new HashMap<ISoldier, Integer>();
		for (DivideUnitComponent duc : divideUnitComponents) {
			if (duc.getUnitAmount() != 0) {
				values.put(duc.getUnit(), duc.getUnitAmount());
			}
		}
		return values;
	}
	
	/**
	 * Gets the second divided troops amount and units.
	 * @return Troops unit and amount.
	 */
	public HashMap<ISoldier, Integer> getTroop2Values() {
		HashMap<ISoldier, Integer> values = new HashMap<ISoldier, Integer>();
		for (DivideUnitComponent duc : divideUnitComponents) {
			if (duc.getNewUnitAmount() != 0) {
				values.put(duc.getUnit(), duc.getNewUnitAmount());
			}
		}
		return values;
			
	}
	
	/**
	 * Gets the entire troop that is subject to division.
	 * @return Troop
	 */
	public ITroop getTroop() {
		return troop;
	}

	/**
	 * Setup GUI depending on units in troop.
	 * @param troop Troop consisting of different units and amount.
	 */
	private void setupGUI(ITroop troop) {
		divideUnitComponentsPanel.setLayout(new GridLayout(troop.getSoldiers().size(), 1));
		
		for (ISoldier s : troop.getSoldiers()) {
			DivideUnitComponent divideUnitComponent = new DivideUnitComponent(s, troop.getAmount(s));
			divideUnitComponents.add(divideUnitComponent);
			divideUnitComponentsPanel.add(divideUnitComponent);
		}
	}
	

	/**
	 * Initializes the GUI.
	 */
	private void initGUI() {
		try {
			
			GroupLayout thisLayout = new GroupLayout((JComponent)this);
			this.setLayout(thisLayout);
			{
				divideUnitComponentsPanel = new JPanel();
			}
			{
				divideTroopButton = new JButton();
				divideTroopButton.setText("Divide troop");
			}
				thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(divideUnitComponentsPanel, 0, 399, Short.MAX_VALUE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
					.addComponent(divideTroopButton, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addContainerGap());
				thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(thisLayout.createParallelGroup()
					    .addComponent(divideUnitComponentsPanel, GroupLayout.Alignment.LEADING, 0, 257, Short.MAX_VALUE)
					    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
					        .addGap(94)
					        .addComponent(divideTroopButton, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 91, Short.MAX_VALUE)))
					.addContainerGap());

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
